package cn.com.guage.dtm.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

public class PDFClient extends JFrame {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int PORT = 8888;

    private JTextField fileNameField;
    private JButton viewButton, editButton, saveButton;
    private JTextArea editTextArea;

    public PDFClient() {
        setTitle("PDF 浏览与编辑客户端");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        fileNameField = new JTextField(20);
        viewButton = new JButton("查看");
        editButton = new JButton("编辑");
        saveButton = new JButton("保存");
        editTextArea = new JTextArea(5, 20);

        add(new JLabel("文件名:"));
        add(fileNameField);
        add(viewButton);
        add(editTextArea);
        add(editButton);
        add(saveButton);

        viewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewPDF();
            }
        });

        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editPDF();
            }
        });

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                savePDF();
            }
        });

        setVisible(true);
    }

    private void viewPDF() {
        String fileName = fileNameField.getText();
        try (Socket socket = new Socket(SERVER_ADDRESS, PORT);
             ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {

            out.writeObject("view");
            out.writeObject(fileName);

            byte[] fileBytes = (byte[]) in.readObject();
            if (fileBytes != null) {
                // 这里可以添加展示 PDF 的逻辑，例如使用第三方库展示
                System.out.println("成功获取 PDF 文件");
            } else {
                System.out.println("文件不存在");
            }
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    private void editPDF() {
        String fileName = fileNameField.getText();
        String text = editTextArea.getText();
        try (Socket socket = new Socket(SERVER_ADDRESS, PORT);
             ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {

            out.writeObject("edit");
            out.writeObject(fileName);
            out.writeObject(text);

            String response = (String) in.readObject();
            System.out.println(response);
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    private void savePDF() {
        String fileName = fileNameField.getText();
        try (Socket socket = new Socket(SERVER_ADDRESS, PORT);
             ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {

            out.writeObject("save");
            out.writeObject(fileName);

            String response = (String) in.readObject();
            System.out.println(response);
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new PDFClient();
    }
}    